package net.exoweb.animate.actions
{
    import flash.display.*;

    import net.exoweb.animate.actions.Action;
    import net.exoweb.animate.common.Maths;

    public class Scale extends Action
    {
        private var _x:Number;
        private var _y:Number;

        public function Scale(x:Number, y:Number, speed:Number)
        {
            super(speed);
            _x = x;
            _y = y;
        }

        public function get x():Number
        {
            return _x;
        }

        public function set x(v:Number):void
        {
            _x = v;
        }

        public function get y():Number
        {
            return _y;
        }

        public function set y(v:Number):void
        {
            _y = v;
        }

        override public function update(obj:DisplayObject, time:Number):void
        {
            var dx:Number = _x - obj.scaleX;
            var dy:Number = _y - obj.scaleY;
            var dv:Number = time * _speed;

            if(((-Maths.EXP < dx) && (dx < Maths.EXP)) ||
               ((-dv < dx) && (dx < dv)))
            {
                obj.scaleX = _x;
            }
            else if(dx > Maths.EXP)
            {
                obj.scaleX += dv;
            }
            else if(dx < -Maths.EXP)
            {
                obj.scaleX -= dv;
            }

            if(((-Maths.EXP < dy) && (dy < Maths.EXP)) ||
               ((-dv < dy) && (dy < dv)))
            {
                obj.scaleY = _y;
            }
            else if(dy > Maths.EXP)
            {
                obj.scaleY += dv;
            }
            else if(dy < -Maths.EXP)
            {
                obj.scaleY -= dv;
            }

            if(obj.scaleX == _x && obj.scaleY == _y)
            {
                complete();
            }
        }
    }
}
